<?php

/**
 * @author huynhtuvinh87
 * @copyright 2013
 */
Yii::import("ext.xupload.models.XUploadForm");

class AdminController extends PermisionController {

    public $layout = '//layouts/column1';

    public function init() {
        Yii::app()->theme = 'admincp';
    }


    public function actionIndex() {
        $this->pageTitle = 'Adminstrator: Quản lý nội dung khác';
        $model = new News('search');
        $model->unsetAttributes(); // clear any default values
        if (isset($_GET['News']))
            $model->attributes = $_GET['News'];
        $this->render('index', array(
            'model' => $model,
        ));
    }

    private function _selectList() {
        $criteria = new CDbCriteria();
        $criteria->condition = 'status=1';
        $categories = NewsCategories::model()->findAll($criteria);
        $listData = array();
        foreach ($categories as $n => $category) {
            $listData[$category->id] = $category->name;
        }
        return $listData;
    }

    public function actionCreate() {
        $this->pageTitle = 'Adminstrator: Thêm mới nội dung';
        $model = new News;
        if (isset($_POST['News'])) {
            $model->attributes = $_POST['News'];
            $image = CUploadedFile::getInstance($model, 'image');
            $model->create_time = time();
            $title = MString::convertToAlias($_POST['News']['name']);
            if (!empty($image)) {
                $change = $title . '.' . $image->getExtensionName();
                if (empty($model->validate)) {
                    if ($image->saveAs('uploads/gallery/' . $change)) {
                        $thumb = Yii::app()->phpThumb->create('uploads/gallery/' . $change);
                        $thumb->resize(230, 230);
                        $thumb->save('uploads/thumb/' . $title . '.' . $image->getExtensionName());
                    }
                }
                $model->image = $change;
            }
            if ($model->save())
                $this->redirect('index');
        }
        $this->render('create', array('model' => $model, 'listData' => $this->_selectList()));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        $this->pageTitle = 'Adminstrator: Cập nhật sản phẩm';
        $model->setScenario('update');
        if (isset($_POST['News'])) {
            $model->attributes = $_POST['News'];
            $image = CUploadedFile::getInstance($model, 'image');
            $title = MString::convertToAlias($_POST['News']['name']);
            if (!empty($image)) {
                $change = $title . '.' . $image->getExtensionName();
                if (empty($model->validate)) {
                    if ($image->saveAs('uploads/gallery/' . $change)) {
                        $thumb = Yii::app()->phpThumb->create('uploads/gallery/' . $change);
                        $thumb->resize(230, 230);
                        $thumb->save('uploads/thumb/' . $title . '.' . $image->getExtensionName());
                    }
                }
                $model->image = $change;
            }
            if ($model->save())
                $this->redirect('index');
        }
        $this->render('create', array('model' => $model, 'listData' => $this->_selectList()));
    }

    public function actionDelete() {
        Yii::app()->clientScript->scriptMap = array('jquery.js' => FALSE, 'jquery.min.js' => FALSE);
        $model = $this->loadModel($_GET['id']);
        $model->delete();
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
    }

    public function actionDeleteAll() {
        $autoIdAll = $_POST['autoId'];
        if (count($autoIdAll) > 0) {
            foreach ($autoIdAll as $autoId) {
                $model = $this->loadModel($autoId);
                $model->delete();
            }
        }
    }

    public function actionStatus() {
        if (!empty($_POST['id'])) {
            $model = $this->loadModel($_POST['id']);
            if ($_POST['status'] == 0)
                $model->status = 1;
            else
                $model->status = 0;
            $model->save();
        }
    }

    public function loadModel($id) {
        $model = News::model()->findByPk($id);
        if ($model === NULL)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}

?>